package com.qili.controller.supervisor;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import lombok.Data;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaoyongke
 * @date 2021/9/28
 * @description
 */
public class TestApp {
    public static void main(String[] args) {
        TestApp  testApp=new TestApp();
        testApp.ss();
    }

public   void  ss(){


    try {

        FileInputStream in = new FileInputStream("D:\\test\\ss.docx");//载入文档
        //获取Word模板，模板存放路径在项目的resources目录下
       // InputStream ins = this.getClass().getResourceAsStream("D:\\test\\材料设备进场检验报告_000_发布日期_工程名称_[子系统名称或包号].docx");
        //注册xdocreport实例并加载FreeMarker模板引擎
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in,
                TemplateEngineKind.Freemarker);
        //创建xdocreport上下文对象
        IContext context = report.createContext();

        //创建要替换的文本变量
        context.put("city", "8888");
        List<aa> goodsList = new ArrayList<aa>();
        for (int i = 0; i <30 ; i++) {
            aa goods1 = new aa();
            goods1.setType("測試"+i);
            goodsList.add(goods1);
        }




//        Goods goods2 = new Goods();
//        goods2.setNum(2);
//        goods2.setType("女装");
//        goods2.setSv(602145);
//        goods2.setSa("651");
//        Goods goods3 = new Goods();
//        goods3.setNum(3);
//        goods3.setType("手机");
//        goods3.setSv(587737);
//        goods3.setSa("866");
//        Goods goods4 = new Goods();
//        goods4.setNum(4);
//        goods4.setType("家具家装");
//        goods4.setSv(551193);
//        goods4.setSa("783");
//        Goods goods5 = new Goods();
//        goods5.setNum(5);
//        goods5.setType("食物饮品");
//        goods5.setSv(528604);
//        goods5.setSa("405");

//        goodsList.add(goods3);
//        goodsList.add(goods4);
//        goodsList.add(goods5);
        context.put("aa", goodsList);

        //创建字段元数据
        FieldsMetadata fm = report.createFieldsMetadata();
        //Word模板中的表格数据对应的集合类型
         fm.load("aa", aa.class, true);
//
        //输出到本地目录
        FileOutputStream out = new FileOutputStream(new File("D://test//商品销售报表.docx"));
        report.process(context, out);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XDocReportException e) {
        e.printStackTrace();
    }

}

@Data
public class aa {

        public   String type;



}



}
